<template>
	<view class="error">
		<view class="error-header">
			<view style="width: 100%; margin: 0 16px;">
				<view style="width: 30%;float: left;    margin-top: 5px;">
					<view class='cell-bd-input' @tap="handleTap('picker1')">{{ value }}
						<image src="../../static/images/menu-down.png" style="height: 27px;width: 24px;vertical-align: middle;"></image>
					</view>
					<lb-picker ref="picker1" v-model="value" mode="selector" :list="cartypeArr" @change="handleChange" @confirm="handleConfirm"
					 @cancel="handleCancel">
					</lb-picker>
				</view>
				<view class="error-seach">
					<view class='search-c' v-bind:class="$store.state.searchStyle">
						<image class='icon search-icon' src='/static/images/shousuo.png' style="width: 20px;height: 18px;"></image>
						<input class='search-input' placeholder-class='search-input-p' placeholder='请输入关键词进行搜索' v-model="keyWord" style="font-size: 14px;"></input>
					</view>
				</view>
			</view>
		</view>
		
		
		
		<view class="error-wrapper" v-if="keyWord">
			<view class="error-item" @click="hanldErrorInfo(item)" v-for="item in list">
				<view class="error-title">{{item.description}}</view>
				<view class="error-text">{{item.level}}</view>
				<!-- <jshopContent :content="item.info" class="error-text"></jshopContent> -->
			</view>
		</view>
		
		<view class="error-wrapper" v-else>
			<view class="error-item" @click="hanldErrorInfo(item)" v-for="item in bankList">
				<view class="error-title">{{item.description}}</view>
				<view class="error-text">{{item.level}}</view>
				<!-- <jshopContent :content="item.info" class="error-text"></jshopContent> -->
			</view>
		</view>
		<uni-load-more :status="loadStatus"></uni-load-more>
	</view>
</template>

<script>
	import xflSelect from '../../components/xfl-select/xfl-select.vue';
	//
	import LbPicker from '@/components/lb-picker'
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue'
	import jshopContent from '@/components/jshop/jshop-content.vue' //视频和文本解析组件
	export default {
		data() {
			return {
				value: '全部设备',
				cartypeArr: [], //类型
				carTypeId: Number,
				car_type: "", //id
				keyWord: "", //搜索关键字
				userInfo: {},
				platform: 'ios',
				loadStatus: 'more',
				page: 1,
				limit: 20, //每页显示几条
				bankList: [], //故障诊断
				refresh: false,
				tiemr: null,
				list:[]
			}
		},
		computed: {
			hasNoData() {
				return !this.list.lenght
			}

		},
		components: {
			xflSelect,
			LbPicker,
			uniLoadMore,
			jshopContent,
		}, //注册为子组件
		onLoad() {
			this.getcarGettype();
			this.getBanklist();
		},
		 watch: {
		        keyWord () {
		            if(this.tiemr){
		                clearTimeout(this.timer)
		            }
		            if(!this.keyWord){
		                this.list = []
		                return
		            }
		            this.tiemr = setTimeout(() => {
		                const result = [];
		                    this.bankList.forEach((value) => {
		                        if(value.info.indexOf(this.keyWord) > -1 || value.description.indexOf(this.keyWord) > -1){
									result.push(value);
								}
		                    });
		                this.list = result
		            }, 100);//当keyWord发生改变,箭头函数就会执行
		        }
		    },
		onReachBottom() {
			if (this.loadStatus === 'more') {
				this.getBanklist()
			}
		},
		methods: {
			search(e, val) {
				console.log(e, val);
				this['val' + val] = e;
			},
			//跳转
			hanldErrorInfo(item) {
				let aa = item;
				let bb = JSON.stringify(aa)
				let cc = encodeURIComponent(bb);

				uni.navigateTo({url:`/pages/service/errorInfo?address=${cc}`})

			},
			handleTap(name) {
				this.$refs[name].show()
			},
			handleChange(item) {
				console.log('change::', item)
			},
			handleConfirm(item) {
				console.log('confirm::', item)
				this.carTypeId = item.index + 1;
				this.page = 1;
				this.bankList=[];
				this.getBanklist()
				console.log(this.page+'page')
			},
			handleCancel(item) {
				console.log('cancel::', item)
			},
			//获取类型
			getcarGettype() {
				this.$api.carGettype({}, res => {
					if (res.status) {
						let car_type = res.data.car_type; //设备类型
						let cartypeArr = car_type.map(item => ({
							id: item.id,
							label: item.name,
							value: item.name
						}))
						this.cartypeArr = cartypeArr;
						
					} else {
						this.$common.errorToShow(res.msg)
					}
				})
			},
			// 获取故障诊断
			getBanklist() {
				let data = {
					page: this.page,
					limit: this.limit,
					car_type: this.carTypeId,
					keyword: this.keyword
				}

				this.loadStatus = 'loading'

				this.$api.cartroubleshooting(data, res => {
					if (res.status) {
						if (this.page >= res.total) {
							// 没有数据了
							this.loadStatus = 'noMore'
						} else {
							// 未加载完毕
							this.loadStatus = 'more'
							this.page++
						}
						this.bankList = [...this.bankList, ...res.data]
					} else {
						this.$common.errorToShow(res.msg)
					}
				})
			},
		}
	}
</script>

<style scoped>
	.error-header {
		height: 50px;
		width: 100%;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		justify-items: center;
		background-color: #F9F9F9;
	}

	.error>>>.list-container {
		top: 80upx !important;

	}

	.error-seach {
		float: right;
		width: 70%;
		height: 32px;
	}

	.error>>>.show-box .list-container .list {
		border: none !important;
		background-color: #f9f9f9 !important;
	}

	.error>>>.show-box {
		border: none !important;
		background-color: #f9f9f9 !important;
		height: 32px !important;
		padding: 0 20% 0 5% !important;
		color: #333333 !important;
		font-size: 14px !important;
	}

	.error>>>.show-box .right-arrow {
		right: 13px;
		color: #DDDDDD !important;
	}

	.error>>>.show-box .list-container .popper__arrow {
		display: none !important;
	}

	.error>>>.show-box .list-container .list .item.active {
		color: #D62222 !important;
		background: none !important;
	}

	.error .search-c {
		background: rgba(255, 255, 255, 1);
		border-radius: 3px;
		border: 1px solid rgba(198, 198, 198, 1);
	}

	.uni-input-placeholder {
		color: #c5c5c5 !important;
	}

	page {
		background-color: #FFFFFF;
		min-height: 100%;
	}

	.error .search-input {
		line-height: 20px !important;
	}

	.error-wrapper {
		display: flex;
		flex-direction: column;
		margin: 0px 17px 12px 17px;
	}

	.error-item {
		padding-bottom: 14px;
		border-bottom: 1px solid #F5F5F5;
	}

	.error-title {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		font-size: 16px;
		color: #333;
		font-weight: bold;
		padding-top: 12px;

	}

	.error-text {
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
		color: #999999;
		font-size: 14px;
		padding-top: 7px;
	}
		
	.search-input-p{
		font-size: 12px;
	}
</style>
